package com.aaa.controller.goods;

import com.aaa.entity.Category;
import com.aaa.service.CategoryService;
import com.aaa.service.impl.CategoryServiceImpl;
import com.aaa.util.IntegerUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ChangeCategoryServlet")
public class ChangeCategoryServlet extends HttpServlet {
    CategoryService categoryService = null;
    @Override
    public void init() throws ServletException {
        categoryService = new CategoryServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Integer status = IntegerUtils.ToInteger(req.getParameter("status"));

        Category category = new Category();
        if(id!=null){
            category.setId(Integer.valueOf(id));
            if(status==1) {
                category.setStatus(0);
            }else{
                category.setStatus(1);
            }

            int i = categoryService.changeStatus(category);
            resp.getWriter().print(new Gson().toJson(category));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
